<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use app\admin\model\Base;
use think\Model;
class Backstage extends Common{

    public function _initialize(){
        parent::_initialize();
    }
    /*
     * 展示页面列表的方法
     *
     * */
    public  function show(){
        $userid= session('user_id');
        $result=Db::table('admin')->where('id='.$userid)->find();
        if($result['password']==md5(123)){
            $this->assign('username',$result['username']);
            $this->assign('userid',$result['id']);
            return $this->fetch('revise');
        }else{
            // 查询状态为1的用户数据 并且每页显示10条数据
            $list = Db::table('admin')->where("deleted",0)->paginate(10);
            // 获取分页显示
            $page = $list->render();
            // 模板变量赋值
            $this->assign('list', $list);
            $this->assign('page', $page);
            // 渲染模板输出
            return $this->fetch();
        }
    }


    /*
     *
     * 管理人个人信息修改以及邮箱绑定
     *
     * */
    public function revise(){
        $newPassword=I('newPassword');
        $checkPassword=I('checkPassword');
        $userid=I('id');
        $email=I('emailbox');
        $result=Db::table('admin')->where('id='.$userid)->find();
        if($newPassword!=$checkPassword ){
            $this->error('两次密码不一致');
        }else if($newPassword==123){
            $this->error('不能和初始密码一样');
        }else if(preg_match('|^[1-9]\d{4,10}@qq\.com$|i',$email)!=true) {
            $this->error('邮箱格式不正确');
        }else if($result['email']==$email){
            $this->error('邮箱已被人注册');
        }else {
          $data=array('password'=>md5($checkPassword),'email'=>$email);
            $rs=Db::table('admin')->where('id='.$userid)->update($data);
            if($rs){
                $this->redirect(url($this->controller.'/show'));
            }else{
                $this->error('失败');
            }
        }
    }

    /*
     *
     * 删除方法，改变状态逻辑删除
     *
     * */
    public function del(){
        $id=isset($_POST['id'])?$_POST['id']:"";
        $data = ['deleted' => 1];
        $del=new Base();
        $del=$del->status('admin',$id,$data);
        echo $del;
    }
    /*
 *
 * 修改管理员信息
 *
 * */

    public function update(){
           $username=I('username');
        if(empty($username)){
            $id=I('id');
            $result=Db::table('admin')->where('id='.$id)->find();
            $results=Db::table('role')->select();
            $this->assign('result',$results);
            $this->assign('admin',$result);
            return  $this->fetch('update');
        }else{
            $id=I('id');
            $selects=$_POST['selects'];
            $rs=explode('.',$selects);
            $pwd=I('newPassword');
            $pwds=I('checkPassword') ;
            if($pwd==$pwds){
            $data = [ 'password' => md5($pwd),'role_id'=>$rs[0],'rolename'=>$rs[1]];
            $rs = Db::table('admin')->where('id='.$id)->update($data);
            if ($rs) {          //设置成功后跳转页面的地址，默认的返回页面是$_SERVER['HTTP_REFERER']
                $this->redirect(url($this->controller.'/show'));
            } else {       //错误页面的默认跳转页面是返回前一页，通常不需要设置
                $this->error('修改管理员失败');
            }
        }
        }


    }

    /**
     * @return mixed
     * 添加后台管理人员的信息
     */
    public function add(){
        $username=I('username');
        if(empty($username)){
            $result=Db::table('role')->select();
            $this->assign('result',$result);
            return $this->fetch('add');
        }else {
                $selects=$_POST['selects'];
                $rs=explode('.',$selects);
                $pwd     =I('pwd');
                $data = ['username' => $username, 'password' => md5($pwd),'role_id'=>$rs[0],'rolename'=>$rs[1]];
                $rs = Db::table('admin')->insert($data);
                if ($rs) {          //设置成功后跳转页面的地址，默认的返回页面是$_SERVER['HTTP_REFERER']
                    $this->redirect(url($this->controller.'/show'));
                } else {       //错误页面的默认跳转页面是返回前一页，通常不需要设置
                    $this->error('添加管理员失败');
                }
            }
        }


}